package ynu.edu.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ynu.edu.service.Img2ImgService;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/controlnet")
@RequiredArgsConstructor
public class ControlNetController {

    private final Img2ImgService img2ImgService;

    @PostMapping("/preprocess")
    public ResponseEntity<Map<String, Object>> preprocessImage(
            @RequestBody Map<String, String> requestBody) {
        try {
            String module = requestBody.get("module");
            String inputImage = requestBody.get("inputImage");
            Integer processorRes = requestBody.get("processorRes") != null ? 
                                   Integer.parseInt(requestBody.get("processorRes")) : null;
            BigDecimal thresholdA = requestBody.get("thresholdA") != null ? 
                                    new BigDecimal(requestBody.get("thresholdA")) : null;
            BigDecimal thresholdB = requestBody.get("thresholdB") != null ? 
                                    new BigDecimal(requestBody.get("thresholdB")) : null;

            if (module == null || inputImage == null) {
                return ResponseEntity.badRequest().body(createErrorResponse("模块名称和输入图片不能为空"));
            }

            log.info("接收到ControlNet预处理请求: module={}, processorRes={}, thresholdA={}, thresholdB={}",
                    module, processorRes, thresholdA, thresholdB);

            String processedImage = img2ImgService.preprocessImage(
                    module, inputImage, processorRes, thresholdA, thresholdB
            );

            if (processedImage != null) {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 200);
                response.put("msg", "预处理成功");
                response.put("processedImage", processedImage);
                response.put("processedImageLength", processedImage.length());
                return ResponseEntity.ok(response);
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                               .body(createErrorResponse("预处理失败"));
            }
        } catch (NumberFormatException e) {
            log.error("参数格式错误", e);
            return ResponseEntity.badRequest().body(createErrorResponse("参数格式错误: " + e.getMessage()));
        } catch (Exception e) {
            log.error("ControlNet预处理接口异常", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                               .body(createErrorResponse("服务器内部错误: " + e.getMessage()));
        }
    }

    private Map<String, Object> createErrorResponse(String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 500);
        response.put("msg", message);
        return response;
    }
} 